package com.hmall.user.interceptor;

import com.hmall.user.pojo.User;
import com.hmall.user.service.IUserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @Author Mingxi Guo
 * @Create 2022-07-17 8:48
 * @Email guomingxi2018@163.cm
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private IUserService userService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userId = request.getHeader("authorization");
        //用feign调用不经过。
        if (userId == null){
            return true;
        }
        User user = userService.getById(Long.valueOf(userId));
        if(Objects.isNull(user)){
            return false;
        }
        ThreadLocal<User> userThreadLocal = new ThreadLocal<>();
        userThreadLocal.set(user);
        return true;
    }
}
